package com.jsm.twentyfour.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * create by jsm on 2019/1/23
 */
public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public JsonUtil() {
    }

    public static String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException var2) {
            return null;
        }
    }

    public static Map <String, Object> toMap(Object obj) {
        Map <String, Object> map = new HashMap();
        Field[] fields = obj.getClass().getDeclaredFields();
        Field[] var3 = fields;
        int var4 = fields.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            Field field = var3[var5];

            try {
                field.setAccessible(true);
                field.get(obj);
                String var8 = field.getName();
            } catch (IllegalAccessException var9) {
                ;
            }
        }

        return map;
    }

    public static <T> T fromJson(String json, Class <T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (IOException var3) {
            return null;
        }

    }
}